package com.schoolhelper.social.mapper;

import com.schoolhelper.social.domain.Friendship;
import com.schoolhelper.social.dto.FriendRequestDTO;
import com.schoolhelper.social.dto.FriendDTO;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface FriendshipMapper {
    
    // 添加好友请求
    @Insert("INSERT INTO friendships (user_id, friend_id, status, remark, create_time, update_time) " +
            "VALUES (#{userId}, #{friendId}, #{status}, #{remark}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertFriendship(Friendship friendship);
    
    // 更新好友状态
    @Update("UPDATE friendships SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateFriendshipStatus(@Param("id") Long id, @Param("status") Integer status);
    
    // 更新好友关系记录
    @Update("UPDATE friendships SET status = #{status}, remark = #{remark}, update_time = NOW() WHERE id = #{id}")
    int updateFriendship(Friendship friendship);
    
    // 查询好友关系
    @Select("SELECT * FROM friendships WHERE user_id = #{userId} AND friend_id = #{friendId}")
    Friendship findFriendship(@Param("userId") Long userId, @Param("friendId") Long friendId);
    
    // 查询用户的所有好友（已同意状态）- 在XML中定义
    List<FriendDTO> findUserFriends(@Param("userId") Long userId);
    
    // 查询待确认的好友请求 - 在XML中定义
    List<FriendRequestDTO> findPendingRequests(@Param("userId") Long userId);
    
    // 查询发出的好友请求 - 在XML中定义
    List<Friendship> findSentRequests(@Param("userId") Long userId);
    
    // 查询现有好友关系 - 在XML中定义
    Friendship findExistingFriendship(@Param("userId") Long userId, @Param("friendId") Long friendId);
    
    // 删除好友关系
    @Delete("DELETE FROM friendships WHERE ((user_id = #{userId} AND friend_id = #{friendId}) " +
            "OR (user_id = #{friendId} AND friend_id = #{userId})) AND status = 1")
    int deleteFriendship(@Param("userId") Long userId, @Param("friendId") Long friendId);
    
    // 检查是否为好友关系
    @Select("SELECT COUNT(*) > 0 FROM friendships " +
            "WHERE ((user_id = #{userId} AND friend_id = #{friendId}) " +
            "OR (user_id = #{friendId} AND friend_id = #{userId})) " +
            "AND status = 1")
    boolean isFriend(@Param("userId") Long userId, @Param("friendId") Long friendId);
    
    // 更新好友备注 - 只更新当前用户的记录
    @Update("UPDATE friendships SET remark = #{remark}, update_time = NOW() " +
            "WHERE user_id = #{userId} AND friend_id = #{friendId} AND status = 1")
    int updateFriendRemark(@Param("userId") Long userId, @Param("friendId") Long friendId, @Param("remark") String remark);
    
    // 为当前用户创建好友备注记录（如果不存在）
    @Insert("INSERT INTO friendships (user_id, friend_id, status, remark, create_time, update_time) " +
            "VALUES (#{userId}, #{friendId}, 1, #{remark}, NOW(), NOW()) " +
            "ON DUPLICATE KEY UPDATE remark = #{remark}, update_time = NOW()")
    int insertOrUpdateFriendRemark(@Param("userId") Long userId, @Param("friendId") Long friendId, @Param("remark") String remark);
}